package com.feihong.txgw.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.feihong.txgw.R;
import com.feihong.txgw.adapter.home.HomeViewpagerAdapter;
import com.feihong.txgw.ui.activity.setting.MessageDetailsActivity;
import com.feihong.txgw.utils.helper.RxHelper;
import com.feihong.txgw.utils.manage.RetrofitManager;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by PVer on 2017/8/18.
 * 首页的dialog显示
 */

public class HomeDialog extends Dialog {


    private ViewPager mViewPager;
    private ImageView iv_close;
    private final String img_view;
    private final String id;
    private Context mContext;


    public HomeDialog(Context context, String img, String aid) {
        //设置自定义Dialog的主题风格
        super(context, R.style.selfDefDialog);
        mContext = context;

        img_view = img;
        id = aid;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //设置Dialog的显示方式：alpha :0->1
        getWindow().setWindowAnimations(R.style.popupAnimation);
        //设置Dialog布局
        setContentView(R.layout.dialog_home_show_layout);
        setCanceledOnTouchOutside(false);
        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mViewPager.setAdapter(createAdapter());
        iv_close = (ImageView) findViewById(R.id.tv_sure);
        iv_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                RetrofitManager.getInstance().homeDialogClick(id).compose(RxHelper.<String>rxSchedulerHelper()).subscribe();

            }
        });
    }

    private PagerAdapter createAdapter() {
        List<View> adapterViews = new ArrayList<>();
//        for (int i = 1; i < 2; i++) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.homedialog_view_layout, null);
        ImageView imageView = (ImageView) view.findViewById(R.id.tv_adapter_title);
        Glide.with(mContext).load(img_view.toString()).into(imageView);

        adapterViews.add(view);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RetrofitManager.getInstance().homeDialogClick(id).compose(RxHelper.<String>rxSchedulerHelper()).subscribe();
                Intent details = new Intent(mContext, MessageDetailsActivity.class);
                details.putExtra("id", id);
                details.putExtra(MessageDetailsActivity.MESSAGE_TYPE, MessageDetailsActivity.MESSAGE_LIST_TYPE);
                mContext.startActivity(details);
                dismiss();
            }
        });

        return new HomeViewpagerAdapter(adapterViews);
    }
}
